using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using Kiwi.Container.Application;
using Kiwi.Container.DependencyInjection;

namespace Kiwi.Container.Wcf
{
    public class DependencyInjectionInstanceProvider : IInstanceProvider
    {
        private readonly Type _instanceType;
        private readonly IEnumerateDependentProperties _dependentProperties;

        public DependencyInjectionInstanceProvider(Type instanceType, IEnumerateDependentProperties dependentProperties)
        {
            _instanceType = instanceType;
            _dependentProperties = dependentProperties;
        }

        #region IInstanceProvider Members

        public object GetInstance(InstanceContext instanceContext, Message message)
        {
            return GetInstance(instanceContext);
        }

        public object GetInstance(InstanceContext instanceContext)
        {
            var container = ContainerManager.Current;
            return container.Construct(_instanceType, _dependentProperties);
        }

        public void ReleaseInstance(InstanceContext instanceContext, object instance)
        {
        }

        #endregion
    }
}